﻿using System.Collections.Generic;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace AspNetCoreIdentity.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        [Authorize]
        public IActionResult Secert()
        {
            return View();
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        public IActionResult Singin(string returnUrl)
        {
            //产生证件
            var schoolClaims = new List<Claim>
            {
                new Claim(ClaimTypes.Name, "007"),
                new Claim(ClaimTypes.SerialNumber, "00701"),
                new Claim("Gender", "男")
            };
            var drivePass = new List<Claim>
            {
                new Claim(ClaimTypes.Name, "007"),
                new Claim(ClaimTypes.SerialNumber, "湘A00001"),
                new Claim("Driver", "GoodHob"),
            };

            var schoolIdentity = new ClaimsIdentity(schoolClaims, "school");
            var govIdentity = new ClaimsIdentity(drivePass, "gov");

            var claimsPrincipal = new ClaimsPrincipal(new[] {schoolIdentity, govIdentity});

            //产生Cookie
            HttpContext.SignInAsync(claimsPrincipal);
            if (string.IsNullOrEmpty(returnUrl)) return View("Index");
            return Redirect(returnUrl);
        }
    }
}
